/*********************************************
 * 表单操作
 ********************************************/
ViewOperate = function( operateConfig, operateDiv) {

	var instance = this;

	this.operateDiv = operateDiv;

	/***************************************************************************
	 * 操作配置
	 **************************************************************************/
	this.operateConfig = operateConfig;

	/*******************************************
	 * 操作按钮集合
	 ******************************************/
	this.operateButtons	=	new Array();
	
	/***************************************************************************
	 * 视图操作绘制
	 **************************************************************************/
	this.operateDraw = function() {

		if (undefined == instance.operateConfig) {
			return;
		}
		
		var length	=	instance.operateConfig.length;
		for ( var x = 0; x < length; x++) {
			var ope = instance.operateConfig[x];
			var link = $("<a href='#' opeIndex="+x+"></a>");
			link.bind("click", function() {
				instance.processOperate(this);
			});
			link.text(ope.name);
			instance.operateButtons[x] = link;
			operateDiv.append(link);

			var icon = ope.icon;
			if (undefined == icon || "" == icon) {
				if ("dataSave" == ope.type) {
					icon = "icon-save";
				} else if ("dataExport" == ope.type) {
					icon = "icon-export";
				} else if ("dataImport" == ope.type) {
					icon = "icon-import";
				} else if ("forward" == ope.type) {
					icon = "icon-forward";
				} else if ("costom" == ope.type) {
					icon = "icon-remove";
				}
			}
			link.linkbutton({
				plain : true,
				iconCls : icon
			});
			if (x != operateConfig.length - 1) {
				operateDiv.append("&nbsp;&nbsp;|&nbsp;&nbsp;");
			}
		}
		instance.refreshOperates();
	};

	/**********************************************
	 * 视图操作处理
	 ***********************************************/
	this.processOperate = function(obj) {
		var level	=	parseInt($(obj).attr("opeIndex"));
		var operate = instance.operateConfig[level];
		var type = operate.type;
		var beforeCheck = instance.onBeforeAction(operate);
		if (false == beforeCheck) {
			return;
		}
		if ("dataExport" == type) {
			alert("待实现");
		} else if ("dataImport" == type) {
			alert("待实现");
		} else if ("forward" == type) {
			instance.onForward(operate);
		} else if ("costom" == type) {
			
		}
		instance.onAfterAction(operate);
	};
	
	this.onForward	=	function(config){
		var forwardUrl	=	config.forwardUrl;
		if(undefined == forwardUrl || "" == forwardUrl){
			alert("跳转地址未配置");
		}else{
			if(forwardUrl.indexOf("http") == 0){
				window.location.href	=	forwardUrl;	
			}else{
				window.location.href	=	application+"/"+forwardUrl;
			}
			
		}
	};
	
	/************************************************
	 * 视图操作刷新
	 ***********************************************/
	this.refreshOperates	=	function(){
		
//		var row	=	listView.selectRow;
//		for(var x=0;x<instance.operateButtons.length;x++){
//			var config	=	instance.operateConfig[x];
//			var readCheck	=	instance.actionReadonlyCheck(config);
//			if(true == readCheck){
//				instance.operateButtons[x].linkbutton('disable');
//			}else{
//				instance.operateButtons[x].linkbutton('enable');
//			}
//			
//			var hideCheck	=	instance.actionHideCheck(config);
//			if(true == hideCheck){
//				instance.operateButtons[x].hide();
//			}
//		}
	};
	
	/*************************************
	 * 操作执行前调用 
	 ************************************/
	this.onBeforeAction		=	function(config){
		if(undefined != config.beforeExecuteScript && "" != config.beforeExecuteScript){
			return ( new Function(  config.beforeExecuteScript ) )();
		}
		return true;
	}
	
	/*************************************
	 * 操作执行前调用 
	 ************************************/
	this.onAfterAction		=	function(config){
		if(undefined != config.afterExecuteScript && "" != config.afterExecuteScript){
			eval(config.afterExecuteScript );
		}
	}

	/**********************************
	 * 操作只读检测
	 * @param config
	 ***********************************/
	this.actionReadonlyCheck	=	function(config){
		if(undefined != config.onlyReadCondition && "" != config.onlyReadCondition){
			return ( new Function(  config.onlyReadCondition ) )();
		}else{
			if("dataEdit" == config.type || "dataDelete" == config.type){
				if(undefined == listView.getSelectedRow()){
					return true;
				}
			}else if("dataBatDelete" == config.type){
				var rows	=	listView.getSelectedRows();
				if(undefined == rows || rows.length == 0){
					return true;
				}
			}
		}
		return false;
	}
	/**********************************
	 * 操作隐藏检测
	 * @param config
	 ***********************************/
	this.actionHideCheck	=	function(config){
		if(undefined != config.hideCondition && "" != config.hideCondition){
			return ( new Function(  config.hideCondition ) )();
		}
		return false;
	}
}